export enum HttpMethod{
    get="get",
    post="post",
    delete="delete",
    put="put"
}
export interface Parameter {
    key:string,
    value?:string
}
const parameter = (key:string,value:string=null) =>{
    return [
        {
            key,value
        }
    ]
}
export interface Helper{
    value:string,
    method:HttpMethod,
    parameters?:Array<Parameter>,
    body?:Object,
}

const create = (uri:string,method:HttpMethod,parameters:Array<Parameter>=[],body:Object=null) :Helper =>{
    return {
        value:uri,
        method:method,
        parameters:parameters,
        body:body,
    }
}

export const createHelpers = (indices:Array<string>,templates:Array<string>):Array<Helper> =>{
    return [
        create("/",HttpMethod.get),
        create("/_cat",HttpMethod.get),
        create("/_template",HttpMethod.get),
        create("/_cat/allocation",HttpMethod.get,parameter("format","json")),
        create("/_cluster/allocation/explain",HttpMethod.get),
        create("/_cluster/reroute",HttpMethod.post,parameter("retry_failed","true"),{}),
        create("/_cat/shards",HttpMethod.get,parameter("format","json")),
        create("/_cat/nodes",HttpMethod.get,parameter("format","json")),
        create("/_cat/master",HttpMethod.get,parameter("format","json")),
        create("/_cat/tasks",HttpMethod.get,parameter("format","json")),
        create("/_cat/indices",HttpMethod.get,parameter("format","json")),
        create("/_cat/segments",HttpMethod.get,parameter("format","json")),
        create("/_cat/count",HttpMethod.get,parameter("format","json")),
        create("/_cat/recovery",HttpMethod.get,parameter("format","json")),
        create("/_cat/health",HttpMethod.get,parameter("format","json")),
        create("/_cat/pending_tasks",HttpMethod.get,parameter("format","json")),
        create("/_cat/aliases",HttpMethod.get,parameter("format","json")),
        create("/_cat/thread_pool",HttpMethod.get,parameter("format","json")),
        create("/_cat/plugins",HttpMethod.get,parameter("format","json")),
        create("/_cat/fielddata",HttpMethod.get,parameter("format","json")),
        create("/_cat/nodeattrs",HttpMethod.get,parameter("format","json")),
        create("/_cat/templates",HttpMethod.get,parameter("format","json")),
        create("/_cluster/health",HttpMethod.get),
        create("/_cluster/settings",HttpMethod.get,parameter("include_defaults","true")),
        create("/_tasks",HttpMethod.get,[{key:"detailed",value:"true"},{key:"actions",value:"*"}]),
        create("/_reindex",HttpMethod.post,[{key:"slices",value:"5"}],{
            "source": {
                "index": "index_source"
            },
            "dest": {
                "index": "index_dest"
            }
        }),
        ...buildByIndices("/_cat/shards/",indices),
        ...buildSettingsByIndices(indices),
        ...buildTemplate(templates),
        ...buildByIndices("/_cat/indices/",indices),
        ...buildByIndices("/_cat/segments/",indices),
        ...buildByIndices("/_cat/count/",indices),
        ...buildByIndices("/_cat/recovery/",indices),
        ...searchByIndices(indices),
        ...deleteByIndices(indices),
    ]
}

const buildByIndices =(prefix:string,indices:Array<string>):Array<Helper> =>{
    const result=[];
    indices.forEach(index=>{
        result.push(create(prefix+index,HttpMethod.get,parameter("format","json")));
    })
    return result;
}

const buildSettingsByIndices =(indices:Array<string>):Array<Helper> =>{
    const result=[];
    indices.forEach(index=>{
        result.push(create("/"+index+"/_settings",HttpMethod.get));
    })
    return result;
}



const buildTemplate =(templates:Array<string>):Array<Helper> =>{
    const result=[];
    templates.forEach(template=>{
        result.push(create("/_template/"+template,HttpMethod.get));
    })
    return result;
}

const searchByIndices =(indices:Array<string>):Array<Helper> =>{
    const result=[];
    indices.forEach(index=>{
        result.push(create("/"+index+"/_search",HttpMethod.post));
    })
    return result;
}

const deleteByIndices =(indices:Array<string>):Array<Helper> =>{
    const result=[];
    indices.forEach(index=>{
        result.push(create("/"+index+"/_delete_by_query",HttpMethod.post,[],{}));
    })
    return result;
}